<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
use think\Loader;
use \app\admin\model\UserAddress;

/**
 * 地址
 */
class Address extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $amodel;

    protected $user_id = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        $this->amodel = new UserAddress;
    }


    /**
     * [addUserAddress 新增收貨地址]
     * @MethodName
     * @Author     Allen
     * @DateTime   2021-10-16T11:08:35+0800
     */
    public function addUserAddress()
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Address');
        if (!$validate->scene('add')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $default_status = isset($rq_data['default_status'])?$rq_data['default_status']:0;

        $data = [];
        $data['user_id'] = $this->user_id;
        $data['name'] = $rq_data['name'];
        $data['mobile'] = $rq_data['mobile'];
        $data['region_type'] = $rq_data['region_type'];
        $data['address'] = $rq_data['address'];
        $data['default_status'] = $default_status;
        $data['createtime'] = time();
        $status = $this->amodel->insertGetId($data);
        if ( !$status ) {
            $this->error('操作失敗');
        }

        if ( $default_status ) {
            $this->amodel
            ->where(['user_id'=>$this->user_id,'id'=>['neq',$status]])
            ->update(['default_status'=>0]);
        }

        $this->success('操作成功');
    }

    /**
     * [updateUserAddress 編輯收貨地址]
     * @MethodName
     * @Author     Allen
     * @DateTime   2021-10-16T11:08:18+0800
     * @return     [type]                   [description]
     */
    public function updateUserAddress()
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Address');
        if (!$validate->scene('edit')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $default_status = isset($rq_data['default_status'])?$rq_data['default_status']:0;

        $info = $this->amodel
                ->field('id,name,mobile,region_type,address,default_status,createtime')
                ->where(['user_id'=>$this->user_id,'id'=>$rq_data['id']])
                ->find();

        if ( !$info ) {
            $this->error('未找到該地址');
        }

        $data = [];
        $data['user_id'] = $this->user_id;
        $data['name'] = $rq_data['name'];
        $data['mobile'] = $rq_data['mobile'];
        $data['region_type'] = $rq_data['region_type'];
        $data['address'] = $rq_data['address'];
        $data['default_status'] = $default_status;

        $status = $this->amodel
                ->where(['id'=>$rq_data['id'],'user_id'=>$this->user_id])
                ->update($data);

        if ( $status === false ) {
            $this->error('操作失敗');
        }

        if ( $default_status ) {
            $this->amodel
            ->where(['user_id'=>$this->user_id,'id'=>['neq',$rq_data['id']]])
            ->update(['default_status'=>0]);
        }
        $this->success('操作成功');
    }

    /**
     * [setDefault 設置默認地址]
     * @MethodName
     * @Author     Allen
     * @DateTime   2021-10-16T11:15:19+0800
     * @param      string                   $value [description]
     */
    public function setDefault($value='')
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Address');
        if (!$validate->scene('default')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $default_status = isset($rq_data['default_status'])?$rq_data['default_status']:0;


        $info = $this->amodel
                ->field('id,name,mobile,region_type,address,default_status,createtime')
                ->where(['user_id'=>$this->user_id,'id'=>$rq_data['id']])
                ->find();

        if ( !$info ) {
            $this->error('未找到該地址');
        }

        $status = $this->amodel
                ->where(['id'=>$rq_data['id'],'user_id'=>$this->user_id])
                ->update(['default_status'=>$default_status]);

        if ( $status === false ) {
            $this->error('操作失敗');
        }

        if ( $default_status ) {
            $this->amodel
            ->where(['user_id'=>$this->user_id,'id'=>['neq',$rq_data['id']]])
            ->update(['default_status'=>0]);
        }
        $this->success('操作成功');
        
    }


    /**
     * [delUserAddress 刪除地址]
     * @MethodName
     * @Author     Allen
     * @DateTime   2021-10-16T11:07:38+0800
     * @return     [type]                   [description]
     */
    public function delUserAddress()
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Address');
        if (!$validate->scene('delete')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $info = $this->amodel
                ->field('id,name,mobile,region_type,address,default_status,createtime')
                ->where(['user_id'=>$this->user_id,'id'=>$rq_data['id']])
                ->find();

        if ( !$info ) {
            $this->error('未找到該地址');
        }

        $status = $this->amodel
                ->where(['id'=>$rq_data['id'],'user_id'=>$this->user_id])
                ->delete();

        if ( $status === false ) {
            $this->error('操作失敗');
        }
        $this->success('操作成功');
    }

    /**
     * [getUserAddressList 收貨地址列表]
     * @MethodName
     * @Author     Allen
     * @DateTime   2021-10-16T11:07:28+0800
     * @return     [type]                   [description]
     */
    public function getUserAddressList()
    {
        $list = $this->amodel
                ->field('id,name,mobile,region_type,address,default_status,createtime')
                ->where(['user_id'=>$this->user_id])
                ->order('id desc')
                ->select();

        $this->success('請求成功',$list);
    }

    /**
     * [getUserAddressDetails 收貨地址詳情]
     * @MethodName
     * @Author     Allen
     * @DateTime   2021-10-16T11:07:19+0800
     * @return     [type]                   [description]
     */
    public function getUserAddressDetails()
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Address');
        if (!$validate->scene('delete')->check($rq_data)) {
            $this->error($validate->getError());
        }
        $info = $this->amodel
                ->field('id,name,mobile,region_type,address,default_status,createtime')
                ->where(['user_id'=>$this->user_id,'id'=>$rq_data['id']])
                ->find();

        $this->success('請求成功',$info?$info:[]);
    }

    /**
     * [getDefaultAddress 獲取默認地址]
     * @MethodName
     * @Author     Allen
     * @DateTime   2021-10-16T13:27:00+0800
     * @param      string                   $value [description]
     * @return     [type]                          [description]
     */
    public function getDefaultAddress($value='')
    {
        $info = $this->amodel
                ->field('id,name,mobile,region_type,address,default_status,createtime')
                ->where(['user_id'=>$this->user_id,'default_status'=>'1'])
                ->find();
        
        $this->success('請求成功',$info?$info:[]);
    }
}
